function showLogin() {
    layui.use('layer',function () {
        layer.tab({
            skin: 'layui-layer-moon', //样式类名
            area: ['500px', '350px'],
            tab: [{
                title: '论坛登陆',
                content:  ' <div id="login" style="">\n' +
                '       <div style="">\n' +
                '               <div class="layui-field-box">\n' +
                '                   <form class="layui-form" action="" style="margin-top: 5%;margin-left: 12%">\n' +
                '                       <div class="layui-form-item">\n' +
                '                           <label class="layui-form-label"><i class=" icon-envelope"></i></label>\n' +
                '                           <div class="layui-input-inline">\n' +
                '                               <input type="text" name="email" required  id="email"  lay-verify="required" placeholder="请输入邮箱" autocomplete="off" class="layui-input">\n' +
                '                           </div>\n' +
                '                       </div>\n' +
                '                       <div class="layui-form-item">\n' +
                '                           <label class="layui-form-label"><i class="icon-lock"></i></label>\n' +
                '                           <div class="layui-input-inline">\n' +
                '                               <input type="password" name="passwd" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">\n' +
                '                           </div>\n' +
                '                           <div class="layui-form-mid layui-word-aux"><a onclick=" checkToRestPass()" style="text-decoration: none"><span id="showPassTip">忘记密码</span></a></div>\n' +
                '                       </div>\n' +
                '                       <div class="layui-form-item">\n' +
                '                           <label class="layui-form-label"></label>\n' +
                '                           <div class="layui-input-inline">\n' +
                '                               <button class="layui-btn  layui-btn-sm" lay-submit lay-filter="formDemo" style="width:100%">立即登录</button>\n' +
                '                           </div>\n' +
                '                       </div>\n' +
                '                       <div class="layui-form-item">\n' +
                '                           <label class="layui-form-label"></label>\n' +
                '                           <div class="layui-input-inline">\n' +
                '                               <button class="layui-btn layui-btn-sm layui-btn-primary" style="width:100%" onclick="checkToRegister();return false;">没账号，立即注册</button>\n' +
                '                           </div>\n' +
                '                       </div>\n' +
                '                   </form>\n' +
                '               </div>\n' +
                '       </div>\n' +
                '    </div>'
            }]
        });
    })
}

function checkToRegister() {
    let str = ' <div id="register" style="">\n' +
        '        <div style="">\n' +
        '            <div class="layui-field-box">\n' +
        '                <form class="layui-form" action="" style="margin-top: 3%;margin-left: 12%">\n' +
        '                    <div class="layui-form-item">\n' +
        '                        <label class="layui-form-label"><i class=" icon-envelope"></i></label>\n' +
        '                        <div class="layui-input-inline">\n' +
        '                            <input type="text" id="email" name="email" required  lay-verify="required" placeholder="请输入邮箱" autocomplete="off" class="layui-input">\n' +
        '                        </div>\n' +
        '                        <div class="layui-form-mid layui-word-aux"><a onclick="getCode()" style="text-decoration: none" id="showMessage">获取邮箱验证码</a></div>\n' +
        '                    </div>\n' +
        '                    <div class="layui-form-item">\n' +
        '                        <label class="layui-form-label"><i class="icon-key"></i></label>\n' +
        '                        <div class="layui-input-inline">\n' +
        '                            <input type="text" id="code" name="code" required lay-verify="required" placeholder="请输入验证码" autocomplete="off" class="layui-input">\n' +
        '                        </div>\n' +
        '                        <div class="layui-form-mid layui-word-aux"><a onclick="" style="text-decoration: none"></a></div>\n' +
        '                    </div>\n' +
        '                    <div class="layui-form-item">\n' +
        '                        <label class="layui-form-label"><i class="icon-lock"></i></label>\n' +
        '                        <div class="layui-input-inline">\n' +
        '                            <input type="password" name="passwd" id="password_1" required lay-verify="required" placeholder="设置密码" autocomplete="off" class="layui-input">\n' +
        '                        </div>\n' +
        '                        <div class="layui-form-mid layui-word-aux"><a onclick="" style="text-decoration: none"></a></div>\n' +
        '                    </div>\n' +
        '                    <div class="layui-form-item">\n' +
        '                        <label class="layui-form-label"><i class=""></i></label>\n' +
        '                        <div class="layui-input-inline">\n' +
        '                            <input type="password" name="passwd_1" id="password_2" required lay-verify="required" placeholder="确认密码" onchange=\'if($("#password_1").val() ===$("#password_2").val()){$("#sign").html("<i class=\\"icon-ok-sign\\" style=\\"color: green\\"></i>")}else {$("#sign").html("<i class=\\"icon-remove-sign\\" style=\\"color: red\\"></i>")}\'  autocomplete="off" class="layui-input">\n' +
        '                        </div>\n' +
        '                        <div class="layui-form-mid layui-word-aux" id="sign"><i class="icon-remove-sign"></i></div>\n' +
        '                    </div>\n' +
        '                    <div class="layui-form-item">\n' +
        '                        <label class="layui-form-label"><i class="icon-user"></i></label>\n' +
        '                        <div class="layui-input-inline">\n' +
        '                            <input type="text" name="nickname" id="nickname" required lay-verify="required" placeholder="设置昵称" autocomplete="off" class="layui-input">\n' +
        '                        </div>\n' +
        '                        <div class="layui-form-mid layui-word-aux"><a onclick="" style="text-decoration: none"></a></div>\n' +
        '                    </div>\n' +
        '                    <div class="layui-form-item">\n' +
        '                        <label class="layui-form-label"></label>\n' +
        '                        <div class="layui-input-inline">\n' +
        '                            <button class="layui-btn  layui-btn-sm" lay-submit lay-filter="formRegister" style="width:100%">立即注册</button>\n' +
        '                        </div>\n' +
        '                    </div>\n' +
        '                    <div class="layui-form-item">\n' +
        '                        <label class="layui-form-label"></label>\n' +
        '                        <div class="layui-input-inline">\n' +
        '                            <button class="layui-btn layui-btn-sm layui-btn-primary" style="width:100%" onclick="checkToLogin();return false;">有账号，去登录</button>\n' +
        '                        </div>\n' +
        '                    </div>\n' +
        '                </form>\n' +
        '            </div>\n' +
        '        </div>\n' +
        '    </div>';
    $("#login").html(str);
}

function showRegister() {
    layui.use('layer',function () {
        layer.tab({
            skin: 'layui-layer-moon', //样式类名
            area: ['500px', '350px'],
            tab: [{
                title: '论坛注册',
                content: ' <div id="register" style="">\n' +
                '        <div style="">\n' +
                '            <div class="layui-field-box">\n' +
                '                <form class="layui-form" action="" style="margin-top: 3%;margin-left: 12%">\n' +
                '                    <div class="layui-form-item">\n' +
                '                        <label class="layui-form-label"><i class=" icon-envelope"></i></label>\n' +
                '                        <div class="layui-input-inline">\n' +
                '                            <input type="text" id="email" name="email" required  lay-verify="required" placeholder="请输入邮箱" autocomplete="off" class="layui-input">\n' +
                '                        </div>\n' +
                '                        <div class="layui-form-mid layui-word-aux"><a onclick="getCode()" style="text-decoration: none" id="showMessage">获取邮箱验证码</a></div>\n' +
                '                    </div>\n' +
                '                    <div class="layui-form-item">\n' +
                '                        <label class="layui-form-label"><i class="icon-key"></i></label>\n' +
                '                        <div class="layui-input-inline">\n' +
                '                            <input type="text" id="code" name="code" required lay-verify="required" placeholder="请输入验证码" autocomplete="off" class="layui-input">\n' +
                '                        </div>\n' +
                '                        <div class="layui-form-mid layui-word-aux"><a onclick="" style="text-decoration: none"></a></div>\n' +
                '                    </div>\n' +
                '                    <div class="layui-form-item">\n' +
                '                        <label class="layui-form-label"><i class="icon-lock"></i></label>\n' +
                '                        <div class="layui-input-inline">\n' +
                '                            <input type="password" name="passwd" id="password_1" required lay-verify="required" placeholder="设置密码" autocomplete="off" class="layui-input">\n' +
                '                        </div>\n' +
                '                        <div class="layui-form-mid layui-word-aux"><a onclick="" style="text-decoration: none"></a></div>\n' +
                '                    </div>\n' +
                '                    <div class="layui-form-item">\n' +
                '                        <label class="layui-form-label"><i class=""></i></label>\n' +
                '                        <div class="layui-input-inline">\n' +
                '                            <input type="password" name="passwd_1" id="password_2" required lay-verify="required" placeholder="确认密码" onchange=\'if($("#password_1").val() ===$("#password_2").val()){$("#sign").html("<i class=\\"icon-ok-sign\\" style=\\"color: green\\"></i>")}else {$("#sign").html("<i class=\\"icon-remove-sign\\" style=\\"color: red\\"></i>")}\'  autocomplete="off" class="layui-input">\n' +
                '                        </div>\n' +
                '                        <div class="layui-form-mid layui-word-aux" id="sign"><i class="icon-remove-sign"></i></div>\n' +
                '                    </div>\n' +
                '                    <div class="layui-form-item">\n' +
                '                        <label class="layui-form-label"><i class="icon-user"></i></label>\n' +
                '                        <div class="layui-input-inline">\n' +
                '                            <input type="text" name="nickname" id="nickname" required lay-verify="required" placeholder="设置昵称" autocomplete="off" class="layui-input">\n' +
                '                        </div>\n' +
                '                        <div class="layui-form-mid layui-word-aux"><a onclick="" style="text-decoration: none"></a></div>\n' +
                '                    </div>\n' +
                '                    <div class="layui-form-item">\n' +
                '                        <label class="layui-form-label"></label>\n' +
                '                        <div class="layui-input-inline">\n' +
                '                            <button class="layui-btn  layui-btn-sm" lay-submit lay-filter="formRegister" style="width:100%">立即注册</button>\n' +
                '                        </div>\n' +
                '                    </div>\n' +
                '                    <div class="layui-form-item">\n' +
                '                        <label class="layui-form-label"></label>\n' +
                '                        <div class="layui-input-inline">\n' +
                '                            <button class="layui-btn layui-btn-sm layui-btn-primary" style="width:100%" onclick="alert(0);return false;">有账号，去登录</button>\n' +
                '                        </div>\n' +
                '                    </div>\n' +
                '                </form>\n' +
                '            </div>\n' +
                '        </div>\n' +
                '    </div>'
            }]
        });
    })
}

function checkToLogin() {
    let str =  ' <div id="login" style="">\n' +
        '       <div style="">\n' +
        '               <div class="layui-field-box">\n' +
        '                   <form class="layui-form" action="" style="margin-top: 5%;margin-left: 12%">\n' +
        '                       <div class="layui-form-item">\n' +
        '                           <label class="layui-form-label"><i class=" icon-envelope"></i></label>\n' +
        '                           <div class="layui-input-inline">\n' +
        '                               <input type="text" name="email" required  lay-verify="required" placeholder="请输入邮箱" autocomplete="off" class="layui-input">\n' +
        '                           </div>\n' +
        '                       </div>\n' +
        '                       <div class="layui-form-item">\n' +
        '                           <label class="layui-form-label"><i class="icon-lock"></i></label>\n' +
        '                           <div class="layui-input-inline">\n' +
        '                               <input type="password" name="passwd" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">\n' +
        '                           </div>\n' +
        '                           <div class="layui-form-mid layui-word-aux"><a onclick="checkToRestPass()" style="text-decoration: none">忘记密码</a></div>\n' +
        '                       </div>\n' +
        '                       <div class="layui-form-item">\n' +
        '                           <label class="layui-form-label"></label>\n' +
        '                           <div class="layui-input-inline">\n' +
        '                               <button class="layui-btn  layui-btn-sm" lay-submit lay-filter="formDemo" style="width:100%">立即登录</button>\n' +
        '                           </div>\n' +
        '                       </div>\n' +
        '                       <div class="layui-form-item">\n' +
        '                           <label class="layui-form-label"></label>\n' +
        '                           <div class="layui-input-inline">\n' +
        '                               <button class="layui-btn layui-btn-sm layui-btn-primary" style="width:100%" onclick="checkToRegister();return false;">没账号，立即注册</button>\n' +
        '                           </div>\n' +
        '                       </div>\n' +
        '                   </form>\n' +
        '               </div>\n' +
        '       </div>\n' +
        '    </div>';
    $("#register").html(str);
    $("#login").html(str);
}

function checkToRestPass() {
    let str =  ' <div id="login" style="">\n' +
        '       <div style="">\n' +
        '               <div class="layui-field-box">\n' +
        '                   <form class="layui-form" action="" style="margin-top: 5%;margin-left: 12%">\n' +
        '                       <div class="layui-form-item">\n' +
        '                           <label class="layui-form-label"><i class=" icon-envelope"></i></label>\n' +
        '                           <div class="layui-input-inline">\n' +
        '                               <input type="text" name="email" id="email" required  lay-verify="required" placeholder="请输入邮箱" autocomplete="off" class="layui-input">\n' +
        '                           </div><div class="layui-form-mid layui-word-aux"><a onclick="forgetPassword()" style="text-decoration: none"><span id="showPassTip">获取验证码</span></a></div>\n' +
        '                       </div><div class="layui-form-item"><label class="layui-form-label">验证码</label> <div class="layui-input-inline"><input type="text" name="code" required lay-verify="required"  autocomplete="off" class="layui-input"></div></div>\n' +
        '                       <div class="layui-form-item">\n' +
        '                           <label class="layui-form-label">新密码</label>\n' +
        '                           <div class="layui-input-inline">\n' +
        '                               <input type="password" name="passwd" id="password_1" required lay-verify="required"  autocomplete="off" class="layui-input">\n' +
        '                           </div>\n' +
        '                          \n' +
        '                       </div><div class="layui-form-item">\n' +
        '                           <label class="layui-form-label"><i class=""></i></label>\n' +
        '                            <div class="layui-input-inline">\n' +
        '                               <input type="password" name="passwd_1" id="password_2" required lay-verify="required" placeholder="确认密码" onchange=\'if($("#password_1").val() ===$("#password_2").val()){$("#sign").html("<i class=\\"icon-ok-sign\\" style=\\"color: green\\"></i>")}else {$("#sign").html("<i class=\\"icon-remove-sign\\" style=\\"color: red\\"></i>")}\'  autocomplete="off" class="layui-input">\n' +
        '                            </div>\n' +
        '                       <div class="layui-form-mid layui-word-aux" id="sign"><i class="icon-remove-sign"></i></div>\n' +
        '                       </div>\n' +
        '                       <div class="layui-form-item">\n' +
        '                           <label class="layui-form-label"></label>\n' +
        '                           <div class="layui-input-inline">\n' +
        '                               <button class="layui-btn  layui-btn-sm" lay-submit lay-filter="resetPass" style="width:100%">立即重置</button>\n' +
        '                           </div>\n' +
        '                       </div>\n' +
        '                       <div class="layui-form-item">\n' +
        '                           <label class="layui-form-label"></label>\n' +
        '                           <div class="layui-input-inline">\n' +
        '                               <button class="layui-btn layui-btn-sm layui-btn-primary" style="width:100%" onclick="checkToLogin();return false;">去登陆</button>\n' +
        '                           </div>\n' +
        '                       </div>\n' +
        '                   </form>\n' +
        '               </div>\n' +
        '       </div>\n' +
        '    </div>';
    $("#login").html(str);
}

function isEmail(str) {
    return /^(\w+)(\.\w+)*@(\w+)(\.\w+)*.(\w+)$/i.test(str)
}

function userRegister() {
    layui.use('form', function() {
        let form = layui.form;
        form.on('submit(formRegister)', function(data){
            //密码检查
            if (data.field['passwd'] !== data.field['passwd_1']) {
                layer.msg("两次密码不一致!")
            } else {
                //验证码检查
                let check = {
                    'email': data.field['email'],
                    'yzcode': data.field['code']
                };
                var result_check = check_code(check);
                if (result_check.code === -1) {
                    layer.msg(result_check.data);
                } else if (result_check.data ===  true) {
                    //验证通过
                    let user = {
                        'email': data.field['email'],
                        'nickname': data.field['nickname'],
                        'passwd': data.field['passwd'],
                    };
                    var result = register(user);
                    console.log(result);
                    if (result.code === 0) {
                        layer.msg(result.message);
                        url = result.data;
                        setTimeout("location.href=url", 1000);
                    } else {
                        layer.msg(result);
                    }
                } else {
                    layer.msg(result_check)
                }
            }
            return false;
        });
    });

}


function restPass() {
    layui.use('form', function() {
        let form = layui.form;
        form.on('submit(resetPass)', function(data){
            //密码检查
            if (data.field['passwd'] !== data.field['passwd_1']) {
                layer.msg("两次密码不一致!")
            } else {
                //验证码检查
                let check = {
                    'email': data.field['email'],
                    'yzcode': data.field['code']
                };
                var result_check = check_code(check);
                if (result_check.code === -1) {
                    layer.msg(result_check.data);
                } else if (result_check.data ===  true) {
                    //验证通过
                    let user = {
                        'email': data.field['email'],
                        'passwd': data.field['passwd'],
                    };
                    var result = update_password_email(user);
                    if (result.code === 0) {
                        layer.msg(result.message);
                    } else {
                        layer.msg(result);
                    }
                } else {
                    layer.msg(result_check)
                }
            }
            return false;
        });
    });


}
var times = 90;
function getCode() {
    layui.use('layer',function () {
        var email = $("#email").val();
        if(isEmail(email)){
            if(times !== 90){
                layer.msg("请不要重复操作，倒计时:"+times+"s");
            }else {
                //发送邮箱账号到后台
                let obj = send_code(email);
                if(obj.code === 0){
                    layer.msg(obj.data);
                }else {
                    layer.msg("验证码发送异常！");
                }
                setTimeout("createCode()",1000);
            };
        }else {
            alert("请输入正确的邮箱地址");
        }
    });
}

function forgetPassword() {
    layui.use('layer',function () {
        var email = $("#email").val();
        if(!$.isEmptyObject(email) && isEmail(email)){
            if(times !== 90){
                layer.msg("请不要重复操作，倒计时:"+times+"s");
            }else {
                //发送邮箱账号到后台
                let obj = send_code_p(email);
                if(obj.code === 0){
                    layer.msg(obj.data);
                }else {
                    layer.msg("验证码发送异常！");
                }
                setTimeout("createCodePass()",1000);
            };
        }else {
            alert("请输入正确的邮箱地址");
        }
    });
}

function createCodePass() {
    times --;
    if(times >= 0){
        setTimeout("createCodePass()",1000);
        let msg = '倒计时:'+times+' s';
        $("#showPassTip").html(msg);
    }else {
        times = 90;
        let msg = '获取验证码';
        $("#showPassTip").html(msg);
    }
}


function createCode() {
    times --;
    if(times >= 0){
        setTimeout("createCode()",1000);
        let msg = '倒计时:'+times+' s';
        $("#showMessage").html(msg);
    }else {
        times = 90;
        let msg = '获取验证码';
        $("#showMessage").html(msg);
    }
}
function userLogin() {
    layui.use('form', function(){
        let form = layui.form;
        //监听提交
        form.on('submit(formDemo)', function(data){
            let user = {
                'email':data.field['email'],
                'passwd':data.field['passwd']
            };
            let obj = login(user);
            if(obj === "密码错误!" || obj === "邮箱不存在!"){
                layer.msg(obj);
            }else if(obj.code === 0){
                layer.msg(obj.data);
                setTimeout("location.reload(true)",2000);
            }
            else {
                layer.msg(obj);
            }
            return false;
        });
    });
}

function userLogout() {
    layui.use('layer',function () {
        let obj = logout();
        if(obj.code === 0){
            layer.msg("注销成功!");
            url = obj.data;
            setTimeout("skip_to(url,'')",2000);
        }else {
            layer.msg("操作失败!");
        }
    });
}